<script>
	import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js';
	import { getUserInfo } from '@/common/api/user.js';
	import { store, mutations } from '@/uni_modules/uni-id-pages/common/store.js';
	import { dzdata, setdzdata, getUserChapterList } from '@/common/js/getData.js';
	import { dzcharacter } from '@/common/js/character.js';
	import { setting, setting_init } from '@/common/js/setting.js';
	import { notify } from '@/common/utils/notify.js';
	const uniIdCo = uniCloud.importObject('uni-id-co', {
		customUI: true
	})
	export default {
		mixins: [mixin],
		globalData: {
			store,
			mutations,
			deviceId: uni.getSystemInfoSync().deviceId,
			dzdata,
			setdzdata,
			dzcharacter,
			dzInfo: {
				name: 'AI.Type 爱打字'
			},
			notify,
			getUserMoreInfo: null,
			setting,
			setting_init
		},
		onLaunch: async function() {
			console.log('App Launch')
			// this.checkLogin();
			setTimeout(() => {
				uni.preloadPage({
					url: '/pages/chapter/chapter'
				})
				uni.preloadPage({
					url: '/pages/map/map'
				})
				uni.preloadPage({
					url: '/pages/character/character'
				})
				uni.preloadPage({
					url: '/pages/course/course'
				})
				uni.preloadPage({
					url: '/pages/learning/learning'
				})
			}, 50)
			this.init();
			this.initList();
			uni.$on('initData', res => {
				this.initList(res);
			})
			setting_init();
			// 监听推送的消息-处理单点登录
			uni.onPushMessage((res) => {
				switch (res.data.payload?.type) {
					case 'USER-LOGIN-SUCCESS':
						const token = uni.getStorageSync('uni_id_token');
						const tokenArr = res.data.payload?.token;
						const findToken = tokenArr.find(item => item === token);
						if (!findToken && token) {
							getApp().globalData.notify({
								type: 'error',
								message: '登录已失效，请重新登录！'
							})
							uni.removeStorageSync('uni-id-pages-userInfo');
							uni.removeStorageSync('uni_id_token');
							uni.removeStorageSync('uni_id_token_expired');
							uni.reLaunch({
								url: '/login'
							})
						}
						break;
					default:
						break;
				}
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			async init() {
				let that = this;
				this.globalData.getUserMoreInfo = this.getUserMoreInfo;
				this.globalData.getUserMoreInfo();
				// 全局处理db错误
				const db = uniCloud.database();
				// 处理错误逻辑
				function onDBError({ code, errCode, message }) {
					that.globalData.notify({
						type: 'error',
						message: `数据库错误：${code} ${message}`
					})
					if (errCode.indexOf('TOKEN_INVALID')>-1) {
						uni.removeStorageSync('uni-id-pages-userInfo');
						uni.removeStorageSync('uni_id_token');
						uni.removeStorageSync('uni_id_token_expired');
					}
				}
				// 绑定clientDB错误事件
				db.on('error', onDBError);
				// 同步客户端push_clientid至device表
				if (uniCloud.onRefreshToken) {
					uniCloud.onRefreshToken(() => {
						uni.getPushClientId({
							success: async function(e) {
								const pushClientId = e.cid;
								await uniIdCo.setPushCid({
									pushClientId
								})
							}
						})
					})
				}
			},
			async getUserMoreInfo() {
				const { uid, tokenExpired } = uniCloud.getCurrentUserInfo();
				if (uniCloud.getCurrentUserInfo().uid && tokenExpired > Date.now()) {
					// 避免前端修改过期时间造成的BUG
					const uni_id_token_expired = uni.getStorageSync('uni_id_token_expired');
					if (uni_id_token_expired != tokenExpired) {
						uni.setStorage({
							key: 'uni_id_token_expired',
							data: tokenExpired
						});
					}
					const { data: moreUserInfo } = await getUserInfo();
					this.globalData.mutations.setUserInfo(moreUserInfo);
					console.log('moreUserInfo===>', moreUserInfo)
				} else {
					uni.removeStorageSync('uni_id_token');
					uni.setStorageSync('uni_id_token_expired', 0)
				}
			},
			async checkLogin() {
				// try {
				// 	await adz.checkLogin();
				// } catch (e) {
				// 	uni.removeStorageSync('uni_id_token');
				// 	uni.removeStorageSync('uni-id-pages-userInfo');
				// 	uni.removeStorageSync('uni_id_token_expired');
				// }
			},
			initList(type) {
				// this.globalData.mutations.setUserInfo(moreUserInfo);
				if (uniCloud.getCurrentUserInfo().uid) {
					switch (type) {
						case 'chapter':
							getUserChapterList();
							break;
						case 'category':
							// getUserCourseList();
							break;
						default:
							// 刷新章节数据
							getUserChapterList();
							// 请求课程列表数据
							// getUserCourseList();
							break;
					}
				}
			}
		}
	}
</script>
<style lang="scss">
	@import '@/uni_modules/uv-ui/index.scss';
	@import '@/common/css/common.scss';
	page {
		min-height: 100%;
		background: #1E1E20;
	}
	body {
		min-width: 900px;
		overflow-x: auto;
	}
	.uni-top-window {
		overflow: visible !important;
	}
	// ::-webkit-scrollbar {
	// 	display: none;
	// 	width: 0 !important;
	// 	height: 0 !important;
	// 	-webkit-appearance: none;
	// 	background: transparent;
	// }
	// .hover {
	// 	cursor: pointer;
	// 	transition: opacity .2s;
	// 	&:hover {
	// 		opacity: 0.9;
	// 	}
	// }
	/*每个页面公共css */
	view, text {
		box-sizing: border-box;
	}
	.uv-flex-row {
		display: flex;
		flex-direction: row;
	}
	.uv-flex-column {
		display: flex;
		flex-direction: column;
	}
	.kuaidazi {
		--my-color-primary: #409eff;
		--my-text-color-primary: #424242;
		--my-text-color-secondary: #424242;
		--my-text-color-regular: #6c6f73;
		--my-text-color-placeholder: #a8abb2;
		--my-text-color-disabled: #c0c4cc;
		--my-fill-color: #fff;
		--my-border-color: #7f8080;
		--my-bg-color: #fff;
		--my-bg-color-page: #fff;
		--my-bg-color-overlay: #fff;
		--my-bg-image: linear-gradient(109.6deg, #dfeaf7 11.2%, #f4f8fc 91.1%);
		--my-header-bg-color: #082c3c;
		--my-header-text-color: #fff;
		--my-header-active-text-color: #ffd04b;
		--my-header-menu-hover-bg-color: #3c4750;
		--my-footer-bg-color: var(--my-header-bg-color);
		--my-footer-text-color: var(--my-header-text-color);
		--my-footer-active-text-color: var(--my-header-active-text-color);
		--my-footer-menu-hover-bg-color: var(--my-header-menu-hover-bg-color);
		background-image: var(--my-bg-image)
	}
	::-webkit-scrollbar {
		width: 6px;
	}
	/* 滚动槽 */
	::-webkit-scrollbar-track {
		border-radius: 10px;
	}
	/* 滚动条滑块 */
	::-webkit-scrollbar-thumb {
		border-radius: 10px;
		background: rgb(49, 52, 63);
	}
	.hidden-scrollbar::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
	}
	.hidden-scrollbar {
		scroll-behavior: smooth;
	}
	span {
		user-select: text;
	}
</style>